//
//  BLInfoPhotoWallController.m
//  Byelone
//
//  Created by 乔小乔 on 2021/2/5.
//

#import "BLInfoPhotoWallController.h"
#import "YJBannerView.h"
#import "BLMeet_Cell_DetailBanner.h"
#import "BLInfoPhotosViewController.h"
#import "BLInfoInputController.h"

@interface BLInfoPhotoWallController ()<YJBannerViewDataSource, YJBannerViewDelegate>
@property (unsafe_unretained, nonatomic) IBOutlet UIButton *btnSubmit;
@property (unsafe_unretained, nonatomic) IBOutlet UIView *viewTop;
@property (nonatomic, strong) YJBannerView *bannerView;
@property (nonatomic, strong) NSArray *bannerArray;//banner数据源
@end

@implementation BLInfoPhotoWallController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"照片墙预览";
    [self createUI];
    [self getPhotos];
}

-(void)getPhotos{
    MJWeakSelf;
    [BLHttpTool postWithPath:BL_USER_PHOTOS params:BL_EMPTYDIC successBlock:^(NSDictionary * _Nonnull obj) {
        
        NSArray * photos = [[obj objectForKey:@"content"] objectForKey:@"list"];
       __block NSMutableArray * tempArray = [NSMutableArray array];
        [photos enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSDictionary * dic = [NSDictionary dictionaryWithDictionary:obj];
            NSString * photoUrl = [dic objectForKey:@"photo"];
            [tempArray addObject:photoUrl];
        }];
        self.bannerArray = [tempArray copy];
//        [weakSelf setBannerArray:[tempArray copy]];
        NSLog(@"%@",obj);
    } failedBlock:^(NSURLSessionDataTask * _Nonnull data, NSError * _Nonnull error) {
        
    }];
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    [self.btnSubmit setGradientBackgroundWithColors:@[[UIColor chColorWithHexString:@"#8881FD"],[UIColor chColorWithHexString:@"#6B71F5"]] locations:@[@(0.0f), @(1.0f)]  startPoint:CGPointMake(0, 0) endPoint:CGPointMake(1, 0)];
}
- (void)createUI{
    [self.viewTop addSubview:self.bannerView];
    
    UIButton *btnRight = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnRight setTitle:@"编辑" forState:UIControlStateNormal];
    [btnRight setTitleColor:[UIColor chColorWithHexString:@"#6C72F5"] forState:UIControlStateNormal];
    [btnRight addTarget:self action:@selector(clickRightBar) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:btnRight];
    self.navigationItem.rightBarButtonItem = item;
}
- (void)clickRightBar{
    if (self.isFromPic) {
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        BLInfoPhotosViewController *vc = [[BLInfoPhotosViewController alloc]init];
        vc.maxSelected = (6 - self.bannerArray.count);
        [self.navigationController pushViewController:vc animated:YES];
        vc.blockPhoto = ^{
            [self getPhotos];
            [[NSNotificationCenter defaultCenter] postNotificationName:Notification_login object:nil];
//            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadData) name:Notification_login object:nil];
        };
    }
}
- (IBAction)clickToInfo:(id)sender {
    BLInfoInputController *vc = [[BLInfoInputController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - banner
-(void)adjustBannerViewWhenCardScreen{
    [self.bannerView adjustBannerViewWhenCardScreen];
}
#pragma mark - YJBannerDelegate

- (NSArray *)bannerViewImages:(YJBannerView *)bannerView{
    return self.bannerArray;
}

- (NSArray *)bannerViewRegistCustomCellClass:(YJBannerView *)bannerView{
    
    return @[[BLMeet_Cell_DetailBanner class]];
    
}
// 自定义cell的复用标识
- (Class)bannerView:(YJBannerView *)bannerView reuseIdentifierForIndex:(NSInteger)index{
    
    return [BLMeet_Cell_DetailBanner class];
}

- (UICollectionViewCell *)bannerView:(YJBannerView *)bannerView customCell:(UICollectionViewCell *)customCell index:(NSInteger)index{
    NSString *url = self.bannerArray[index];
    BLMeet_Cell_DetailBanner *cell = (BLMeet_Cell_DetailBanner*)customCell;
    [cell reloadWithUrl:url];
    return cell;
}

- (CGFloat)bannerViewFooterViewHeight:(YJBannerView *)bannerView{
    
    return  0.01;
}

- (void)bannerView:(YJBannerView *)bannerView didSelectItemAtIndex:(NSInteger)index{
    
}
#pragma mark - get&&set
- (YJBannerView *)bannerView{
    if (!_bannerView) {
        _bannerView = [YJBannerView bannerViewWithFrame:CGRectMake(0, 0, BL_WINDOW_WIDTH, BL_WINDOW_WIDTH*42/75) dataSource:self delegate:self emptyImage:[UIImage imageNamed:@"defaultImage"] placeholderImage:[UIImage imageNamed:@"defaultImage"] selectorString:@"sd_setImageWithURL:placeholderImage:"];
        _bannerView.pageControlStyle = PageControlCustom;
        _bannerView.pageControlDotSize = CGSizeMake(10, 4);
        _bannerView.customPageControlHighlightImage = [UIImage imageNamed:@"pageControlS"];
        _bannerView.customPageControlNormalImage = [UIImage imageNamed:@"pageControlN"];
        _bannerView.pageControlPadding = 2;
        _bannerView.pageControlAliment = PageControlAlimentCenter;
        _bannerView.pageControlBottomMargin = 70;
        _bannerView.autoDuration = 5;
    }
    return _bannerView;
}

- (void)setBannerArray:(NSArray *)bannerArray{
    _bannerArray = bannerArray;
    [self.bannerView reloadData];
//    if (_defoultImg) {
//        _defoultImg.hidden = YES;
//    }
}
@end
